package org.chen.model.elastic;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.chen.model.vo.ArticleVO;
import org.chen.utils.ArticleConverter;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ArticlePageResult {
    private List<ArticleVO> records;
    private long total;
    private int page;
    private int size;

    public static ArticlePageResult of(Page<ArticleIndex> page) {
        List<ArticleVO> records = page.getContent().stream()
                .map(ArticleConverter::toVOFromIndex)
                .collect(Collectors.toList());

        return ArticlePageResult.builder()
                .records(records)
                .total(page.getTotalElements())
                .page(page.getNumber() + 1)
                .size(page.getSize())
                .build();
    }
}
